package com.xiaoy.core.bpm.engine.activiti.activiti5;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.activiti.engine.impl.RepositoryServiceImpl;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.impl.pvm.process.ProcessDefinitionImpl;
import org.activiti.engine.repository.ProcessDefinition;

public class ProcessInstanceAction extends EngineActiviti5 {
	private List<ActivityImpl> actImpls = new ArrayList<ActivityImpl>();

	public List<ActivityImpl> getActImpls() {
		return actImpls;
	}

	public void setActImpls(List<ActivityImpl> actImpls) {
		this.actImpls = actImpls;
	}

	/** * 显示流程图 * * @return * @throws Exception */
	public String getProcessPic() throws Exception {
		// String taskId = //
		// "2901";//request.getParameter("taskId");3016,552,3020
		String procDefId = "";// request.getParameter("procDefId");
		ProcessDefinition procDef = repositoryService
				.createProcessDefinitionQuery().processDefinitionId(procDefId)
				.singleResult();
		String diagramResourceName = procDef.getDiagramResourceName();
		InputStream imageStream = repositoryService.getResourceAsStream( procDef.getDeploymentId(), diagramResourceName);
		// request.setAttribute("inputStream", imageStream);
		return "SUCCESS";
	}

	/** * 获取跟踪信息 * * @return * @throws Exception */
	public String getProcessMap() throws Exception {
		String procDefId = "";// request.getParameter("procDefId");
		String proInstId = "";// request.getParameter("procInstId");
		ProcessDefinition processDefinition = repositoryService
				.createProcessDefinitionQuery().processDefinitionId(procDefId)
				.singleResult();
		ProcessDefinitionImpl pdImpl = (ProcessDefinitionImpl) processDefinition;
		String processDefinitionId = pdImpl.getId();// 流程标识
		ProcessDefinitionEntity def = (ProcessDefinitionEntity) ((RepositoryServiceImpl) repositoryService)
				.getDeployedProcessDefinition(processDefinitionId);
		List<ActivityImpl> activitiList = def.getActivities();
		// 获得当前任务的所有节点 /****/
		List<String> ActiveActivityIds = runtimeService
				.getActiveActivityIds(proInstId);
		for (String activeId : ActiveActivityIds) {
			for (ActivityImpl activityImpl : activitiList) {
				String id = activityImpl.getId();
				if (activityImpl.isScope()) {
					if (activityImpl.getActivities().size() > 1) {
						List<ActivityImpl> subAcList = activityImpl
								.getActivities();
						for (ActivityImpl subActImpl : subAcList) {
							String subid = subActImpl.getId();
							System.out.println("subImpl:" + subid);
							if (activeId.equals(subid)) {// 获得执行到那个节点
								actImpls.add(subActImpl);
								break;
							}
						}
					}
				}
				if (activeId.equals(id)) {// 获得执行到那个节点
					actImpls.add(activityImpl);
					System.out.println(id);
				}
			}
		}
		/****/
		return "SUCCESS";
	}
}